﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is LunAS Library.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon)  <ekameleon@gmail.com>.
  Portions created by the Initial Developer are Copyright (C) 2004-2012
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
  Alternatively, the contents of this file may be used under the terms of
  either the GNU General Public License Version 2 or later (the "GPL"), or
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  in which case the provisions of the GPL or the LGPL are applicable instead
  of those above. If you wish to allow use of your version of this file only
  under the terms of either the GPL or the LGPL, and not to allow others to
  use your version of this file under the terms of the MPL, indicate your
  decision by deleting the provisions above and replace them with the notice
  and other provisions required by the LGPL or the GPL. If you do not delete
  the provisions above, a recipient may use your version of this file under
  the terms of any one of the MPL, the GPL or the LGPL.
  
*/

package lunas.managers
{
    import system.events.EventListener;

    import flash.display.InteractiveObject;
    import flash.events.Event;
    import flash.utils.getTimer;
    
    /**
     * Manages the tap gesture from a interactive object. A tap gesture can take on different contexts dependant on target device.
     */
    public class TapManager
    {
        /**
         * Creates a new TapManager instance.
         * @param threshold The maximum amount of time that relates to the time for a tap gesture (in milliseconds).
         */
        public function TapManager( threshold:int = 500 )
        {
            _threshold = threshold ;
        }
        
        /**
         * Returns true if the specific display is register in the manager.
         */
        public function available( display:InteractiveObject ):Boolean
        {
            return _available && ( display == _display );
        }
        
        /**
         * Register a specific interactive display object to manage a tap gesture event.
         * @param display The interactive display object that dispatches events recognized as a tap.
         * @param listener a Function or an EventListener to delegate handler once the tap gesture is received.
         */
        public function register( display:InteractiveObject, listener:* ):void
        {
            _available = true;
            _display   = display;
            
            if( listener is EventListener )
            {
                _listener = (listener as EventListener).handleEvent ; 
            }
            else if( listener is Function )
            {
                _listener = listener as Function ;
            }
            else
            {
                _listener  = null ;
            }
        }
        
        /**
         * Unregister the manager.
         */
        public function unregister( display:InteractiveObject = null ):void
        {
            _listener  = null;
            _display   = null;
            _available = false;
        }
        
        /**
         * @private
         */
        protected var _available:Boolean ;
        
        /**
         * @private
         */
        protected var _display:InteractiveObject;
        
        /**
         * @private
         */
        protected var _listener:Function;
        
        /**
         * @private
         */
        protected var _startTime:int;
        
        /**
         * @private
         */
        protected var _threshold:int ;
        
        /**
         * Invoked when start a touch event. 
         * Templated method to manage time between gestures to be used and/or overridden in subclass. 
         * @private
         */
        protected function touchBegin( e:Event ):void
        {
            if( e.target != _display )
            {
                return;
            }
            
            _startTime = getTimer();
        }
        
        /**
         * Invoked for end of a touch event. 
         * Templated method to manage time between gestures to be used and/or overridden in subclass. 
         * @private
         */
        protected function touchFinish( e:Event ):void
        {
            if( !_listener || (e.target != _display) )
            {
                return ;
            }
            if( getTimer() - _startTime <= _threshold )
            {
                _listener( e ) ;
            }
        }
    }
}
